import re

from lxml import etree
import time
import datetime
from Save_database import Save_database

class Parse_page(object):
    def __init__(self, driver, db):
        self.driver = driver
        self.db = db
        self.sd = Save_database(self.db)

    def get_current_week(self):
        monday, sunday = datetime.date.today(), datetime.date.today()
        one_day = datetime.timedelta(days=1)
        while monday.weekday() != 0:
            monday -= one_day
        while sunday.weekday() != 6:
            sunday += one_day
        # 返回当前的星期一和星期天的日期
        # print(monday, sunday)
        return monday, sunday

    def parse_page(self, first_category, second_category):
        monday, sunday = self.get_current_week()
        week = str(monday)+str('/')+str(sunday)
        js = "var q=document.documentElement.scrollTop=100000"  # 用于页面滑动的js
        self.driver.execute_script(js)  # 把页面滑动到最下方
        time.sleep(2)
        html = etree.HTML(self.driver.page_source)
        self.root_tree = html.xpath('//*[@id="J_goodsList"]/ul/li')
        for rt in self.root_tree:
            sku = str(rt.xpath('@data-sku')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            spu = str(rt.xpath('@data-spu')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            pid = str(rt.xpath('@data-pid')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            product_url = str(rt.xpath('div/div[1]/a/@href')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            sale_price = str(rt.xpath('div/div[3]/strong/i/text()')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            product_title = str(rt.xpath('div/div[4]/a/em//text()')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            comment_num = str(rt.xpath('div/div[5]/strong/a/text()')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '').replace('+', '')
            if '万' in comment_num:
                comment_num = re.match('\d*', comment_num)
                comment_num = int(comment_num.group(0)) * 10000
            shop_name = str(rt.xpath('div/div[7]/span/a/text()')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            shop_url = str(rt.xpath('div/div[7]/span/a/@href')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            shop_promotion = str(rt.xpath('div/div[8]/i/text()')).replace('\'', '').replace('[', '').replace(']', '').replace(',', '')
            if self.sd.is_save_sku(sku, week):
                self.sd.save_outter_data(sku.strip(), spu.strip(), pid.strip(), product_url.strip(), sale_price.strip(), \
                  product_title.strip(), comment_num, shop_name.strip(), shop_url.strip(), \
                  shop_promotion.strip(), first_category.strip(), second_category.strip(), week.strip())
            else:
                print('不入库')
            time.sleep(1)
            # print(sku[0].strip(), spu[0].strip(), pid[0].strip(), product_url[0].strip(), sale_price[0].strip(), \
            #       product_title.strip(), comment_num[0].strip(), shop_name[0].strip(), shop_url[0].strip(), \
            #       shop_promotion[0].strip(), first_category.strip(), second_category.strip(), week.strip())
